<?php

namespace App\Http\Middleware;

use App\Facades\Data;
use App\Helpers\SystemHelper;
use Closure;
use Illuminate\Http\Request;

class SassSignCheck
{
    private $key='ib7kaEeYlHC6';

    /**
     * @param Request $request
     * @param Closure $next
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function handle(Request $request, Closure $next)
    {
        $postData=$request->post();
        if(!$request->has(['sign','timestamp'])){
            Data::set(SystemHelper::PARAM_ERROR,SystemHelper::PARAM_ERROR_MSG);
            return response(Data::returnMsg());
        };
        $sign=$postData['sign'];
        unset($postData['sign']);
        ksort($postData);
        $str='';
        foreach($postData as $key=>$p){
            $str.=$key.$p;
        }
        if(md5($str.$this->key)!==$sign){
            Data::set(SystemHelper::HTTP_NO_AUTH,SystemHelper::HTTP_NO_AUTH_MSG);
            return response(Data::returnMsg());
        }
        return $next($request);
    }
}
